<template>
    <div class="shopcar_cantainer">
        <div class="mui-card" v-for="(item,i) in goodsList" :key="item.id">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <mt-switch @change="selected(item.id,$store.state.car[i].selected)" v-model="$store.state.car[i].selected"></mt-switch>
                    <img :src="item.thumb_path" alt="">
                    <div class="info">
                        <h1>{{item.title}}</h1>
                        <p class="item">
                            <span class="price">{{item.sell_price}}元</span>
                            <number :goodsId="item.id" :initCount="$store.getters.getGoodsCount[item.id]"></number>
                            <a @click.prevent="remove(i,item.id)" href="#">删除</a>
                        </p>
                    </div>
                </div>
            </div>
        </div>


        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner jiesuan">
                    <div class="left">
                        <p>总计（不含运费）</p>
                        <p>已勾选商品 <span class="red">
                            {{$store.getters.getTotal.count}}</span> 件，
                            总价 <span class="red">
                                ￥{{$store.getters.getTotal.allPrice}}
                            </span>
                        </p>
                    </div>
                    <mt-button type="danger">去结算</mt-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import number from '../subcomponents/shopcar_number.vue'
    export default {
        name: "shopCart",
        data(){
            return {
                goodsList:[]
            }
        },
        components:{
            number
        },
        methods:{
            //1.获取购物车商品列表
            getGoodsList(){
                //获取商品的id
                let idArr = [];
                this.$store.state.car.forEach( item => idArr.push((item.id)));
                if (idArr.length <= 0) return false;
                this.$http.get('api/goods/getshopcarlist/'+idArr.join(',')).then( result => {
                    if (result.body.status == 0){
                        // console.log(result.body.message)
                        this.goodsList = result.body.message;
                    }
                })
            },
            //2.点击删除
            remove(i,id){
                this.goodsList.splice(i,1);
                this.$store.commit('removeGoods',id)
            },
            //3.更新状态
            selected(id,selected){
                this.$store.commit('updateSelected',{id:id,selected:selected})
            }
        },
        created(){
            this.getGoodsList();
        }
    }
</script>

<style scoped lang="less">
    .shopcar_cantainer{
        background-color: #eee;
        overflow: hidden;
        .mui-card-content-inner{
            display: flex;
            /*纵向居中*/
            align-items: center;
            img{
                width: 50px;
            }
            h1{
                font-size: 14px;
            }
            .info{
                .price{
                    color: red;
                    font-weight: bolder;
                }
                .item{
                    display: flex;
                    align-items: center;
                }
            }
        }
        .jiesuan {
            display: flex;
            justify-content: space-between;
            align-items: center;
            .red {
                color: red;
                font-weight: bold;
                font-size: 16px;
            }
        }
    }
</style>